package com.gs.process.core;

import com.alibaba.fastjson.JSON;
import com.gs.process.core.configuration.ProcessConfig;
import com.gs.process.core.user.ProcessUserService;
import com.gs.process.core.utils.HttpTokenUtil;
import com.gs.process.model.Approval;
import com.shallowUniverse.http.HttpApiService;
import com.shallowUniverse.http.pojo.HttpResult;
import com.shallowUniverse.model.Params;
import com.shallowUniverse.model.Result;
import lombok.Data;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

/**
 * @author: Shuai Guo
 * @description:
 * @date: 2021/9/27
 * @version: 1.0
 */
@Data
public class ApprovalApi {

    private static final String SERVICE = "/approval";
    private HttpApiService httpApiService;
    private ProcessConfig processConfig;
    private ProcessUserService processUserService;
    private DataSource dataSource;

    public Result<List<Approval>> findByUser(Params<Approval> params) {
        return commonQuery("/find/user", params);
    }

    public Result<List<Approval>> findByRole(Params<Approval> params) {
        return commonQuery("/find/role", params);
    }

    public Result<List<Approval>> find(Params<Approval> params) {
        return commonQuery("/find", params);
    }

    public Result<Long> save(Approval approval) {
        try {
            HttpResult httpResult = httpApiService.doPost(processConfig.getServerUrl() + SERVICE + "/save", HttpTokenUtil.tokenHeaders(), approval);
            //转换
            return (Result<Long>) JSON.parseObject(httpResult.getBody(), Result.class);
        } catch (Exception e) {
            e.printStackTrace();
            Result<Long> result = new Result<>();
            result.setCode(500).setMessage("调用失败");
            return result;
        }
    }

    private Result<List<Approval>> commonQuery(String mapping, Params<Approval> params) {
        try {
            HttpResult httpResult = httpApiService.doPost(processConfig.getServerUrl() + SERVICE + mapping, HttpTokenUtil.tokenHeaders(), params);
            //设置目标表的数据
            Result requestResult = JSON.parseObject(httpResult.getBody(), Result.class);
            Result<List<Approval>> result = new Result<>();
            result.setBody(JSON.parseArray(requestResult.getBody().toString(),Approval.class));
            //编写查询语句
            String sql = "select * from %s.%s where %s = '%s'";
            //定义runner对象
            QueryRunner runner = new QueryRunner(dataSource);
            result.getBody().forEach(item -> {
                try {
                    item.setTargetData(
                            runner.query(
                                    String.format(
                                            sql,processConfig.getBusinessDatabase(),
                                            item.getTarget(),
                                            item.getPrimaryKey(),
                                            item.getTargetId()
                                    ),new MapHandler()
                            )
                    );
                } catch (SQLException throwable) {
                    throwable.printStackTrace();
                }
            });

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            Result<List<Approval>> result = new Result<>();
            result.setCode(500).setMessage("调用失败");
            return result;
        }
    }
}
